<?php
/**
 * Created by PhpStorm.
 * User: cape
 * Date: 2017/11/14
 * Time: 10:08
 */

/**
 * @param $filename
 * @return string
 */
function getExt($filename)
{
    return strtolower(pathinfo($filename, PATHINFO_EXTENSION));
}

/**
 * @param $num1
 * @param $num2
 * @param $op
 * @return string
 */
function calc($num1, $num2, $op)
{
    if (!is_numeric($num1) || (!is_numeric($num2))) {
        exit('非法的操作。。。。');

    }

    switch ($op) {
        case '+':
            $res = $num1 + $num2;
            break;
        case '-':
            $res = $num1 - $num2;
            break;
        case '*':
            $res = $num1 * $num2;
            break;
        case '/':
            if ($num2 != 0) {
                $res = $num1 / $num2;
            } else {
                exit('0不能当作除数');
            }
        case '%':
            $res = $num1 % $num2;
            break;

    }
    return "{$num1}{$op}{$num2}={$res}";
}

echo calc(1, 2, '+');


/**
 * @param string $del
 * @param string $del2
 * @param string $del3
 * @return string
 */
function getDateStr($del = '年', $del2 = '月', $del3 = '日')
{

    $dayArr = array('日', '一', '二', '三', '四', '五', '六');
    $day = date('w');
    return date("Y{$del}m{$del2}d{$del3} 星期") . $dayArr[$day];
}

echo '<hr/>';
echo getDateStr();

echo '<hr/>';


function xrange($start, $limit, $step = 1)
{
    if ($start < $limit) {
        if ($step <= 0) {
            throw new LogicException('Step mus tbe +ve');
        }

        for ($i = $start; $i <= $limit; $i += $step) {
            yield $i;
        }

    } else {
        if ($step >= 0) {
            throw  new LogicException('Stemp must be -ve');
        }

        for ($i = $start; $i >= $limit; $i += $step) {
            yield $i;
        }
    }
}


echo 'Single digit odd numbers from range();';
foreach (range(1, 9, 2) as $number) {
    echo "$number".'<br/>';
}

echo "\n";

foreach (xrange(1, 9, 2) as $number) {
    echo "$number";
}












